package com.zjp;

import com.google.common.base.Joiner;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import org.junit.jupiter.api.Test;

import java.util.List;
import java.util.Map;

public class JoinerTest {


    @Test
    public void t1() {
        // 使用 on 连接字符串
        List<String> lists = Lists.newArrayList("a", "b", "g", "8", "9");
        String result = Joiner.on(",").join(lists);
        System.out.println(result);
    }

    @Test
    public void t2() {
        List<String> lists = Lists.newArrayList("a", "b", "g", null, "8", "9");
        // 有 null 会报 java.lang.NullPointerException
        // 使用 skipNulls 跳过
        String result1 = Joiner.on(",").skipNulls().join(lists);
        System.out.println(result1);

        // 使用 useForNull 对 null 进行替换
        String result2 = Joiner.on(",").useForNull("+").join(lists);
        System.out.println(result2);
    }

    @Test
    public void t3() {
        // key value 连接符
        Map<Integer, String> maps = Maps.newHashMap();
        maps.put(1, "哈哈");
        maps.put(2, "嘻嘻");
        String result = Joiner.on(",").withKeyValueSeparator(":").join(maps);
        System.out.println(result);
        System.out.println(maps);
    }

}
